package com.zbkj.crmeb.merchant.admin.controller;

import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.member.model.MemberPointsManagement;
import com.zbkj.crmeb.member.service.IMemberPointsManagementService;
import com.zbkj.crmeb.store.dto.ChannelIntegralConfigDTO;
import com.zbkj.crmeb.store.dto.ChannelIntegralProductConfigDTO;
import com.zbkj.crmeb.store.model.OrderInfoDelivery;
import com.zbkj.crmeb.store.request.ChannelIntegralExchangeRequest;
import com.zbkj.crmeb.store.request.OrderInfoDeliveryRequest;
import com.zbkj.crmeb.store.request.StoreOrderSendRequest;
import com.zbkj.crmeb.store.response.ChannelIntegralExchangeResponse;
import com.zbkj.crmeb.store.service.ChannelIntegralConfigService;
import com.zbkj.crmeb.store.service.ChannelIntegralExchangeService;
import com.zbkj.crmeb.store.service.ChannelIntegralProductConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 功能描述: 商户积分设置
 * @Author: wangzh
 * @Date: 2023/2/20 14:55
 */
@RestController
@RequestMapping("api/admin/mer/integral/store/config")
@Api(tags = "积分商城")
public class MerChannelIntegralStoreController extends AdminBaseController{
    @Autowired
    private ChannelIntegralConfigService channelIntegralConfigService;
    @Autowired
    private IMemberPointsManagementService memberPointsManagementService;
    @Autowired
    private ChannelIntegralProductConfigService channelIntegralProductConfigService;
    @Autowired
    private ChannelIntegralExchangeService channelIntegralExchangeService;

    @PostMapping("/integral")
    @ApiOperation(value = "商户保存积分设置")
    public CommonResult<Boolean> saveIntegralConfig(@RequestBody ChannelIntegralConfigDTO dto, HttpServletRequest request){
        dto.setMerId(getMerId(request));
        return CommonResult.success(channelIntegralConfigService.saveIntegralConfig(dto));
    }

    @GetMapping("/integral")
    @ApiOperation(value = "获取当前商户积分设置")
    public CommonResult<ChannelIntegralConfigDTO> getIntegralConfig(HttpServletRequest request){
        return CommonResult.success(channelIntegralConfigService.getIntegralConfig(getMerId(request)));
    }

    @GetMapping("/pointsEnable")
    @ApiOperation(value = "获取当前商户积分设置")
    public CommonResult<Boolean> getPointsEnableConfig(HttpServletRequest request){
        return CommonResult.success(memberPointsManagementService.getPointsEnableConfig(getMerId(request)));
    }

    @GetMapping("/product")
    @ApiOperation(value = "分页查询商品设置信息")
    public CommonResult<CommonPage<ChannelIntegralProductConfigDTO>> getIntegralProductConfig(ChannelIntegralProductConfigDTO dto, PageParamRequest pageParamRequest, HttpServletRequest request) {
        dto.setMerId(getMerId(request));
        return CommonResult.success(CommonPage.restPage(channelIntegralProductConfigService.getIntegralProductConfig(dto, pageParamRequest, false)));
    }

    @PostMapping("/product")
    @ApiOperation(value = "新增或修改积分商品设置")
    public CommonResult<Boolean> saveOrUpdateIntegralProduct(@RequestBody ChannelIntegralProductConfigDTO dto, HttpServletRequest request) {
        dto.setMerId(getMerId(request));
        return CommonResult.success(channelIntegralProductConfigService.saveOrUpdateIntegralProduct(dto));
    }

    @PutMapping("/product/status/{id}/{status}")
    @ApiOperation(value = "上架/下架")
    public CommonResult<Boolean> updateStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        return CommonResult.success(channelIntegralProductConfigService.updateStatus(id, status));
    }

    @GetMapping("/product/name")
    @ApiOperation(value = "商品设置积分商品下拉列表")
    public CommonResult<List<ChannelIntegralProductConfigDTO>> getList(HttpServletRequest request) {
        return CommonResult.success(channelIntegralProductConfigService.getList(getMerId(request)));
    }

    @DeleteMapping("/product/{id}")
    @ApiOperation(value = "删除积分商品")
    public CommonResult<Boolean> deleteIntegralProduct(@PathVariable Integer id) {
        return CommonResult.success(channelIntegralProductConfigService.deleteIntegralProduct(id));
    }

    @GetMapping
    @ApiOperation(value = "获取商户积分商城状态")
    public CommonResult<Boolean> getIntegralStoreStatus(HttpServletRequest request) {
        return CommonResult.success(channelIntegralConfigService.getIntegralStoreStatus(getMerId(request)));
    }

    @PutMapping("/{status}")
    @ApiOperation(value = "开启/关闭积分商城")
    public CommonResult<Boolean> updateIntegralStoreStatus(@PathVariable Boolean status, HttpServletRequest request) {
        return CommonResult.success(channelIntegralConfigService.updateIntegralStoreStatus(status, getMerId(request)));
    }

    @GetMapping("/exchange")
    @ApiOperation(value = "兑换记录")
    public CommonResult<CommonPage<ChannelIntegralExchangeResponse>> getList(ChannelIntegralExchangeRequest request, HttpServletRequest servletRequest) {
        return CommonResult.success(CommonPage.restPage(channelIntegralExchangeService.getList(request, getMerId(servletRequest), false)));
    }

    @PostMapping("/delivery")
    @ApiOperation(value = "发货")
    public CommonResult<Boolean> send(@RequestBody StoreOrderSendRequest request) {
        return CommonResult.success(channelIntegralExchangeService.sendProduct(request));
    }

    @GetMapping("/delivery")
    @ApiOperation("发货记录")
    public CommonResult<List<OrderInfoDelivery>> getDeliveryList(@Validated OrderInfoDeliveryRequest request) {
        return CommonResult.success(channelIntegralExchangeService.getDeliveryList(request));
    }
}
